<?php
/**
 * [PHPFOX_HEADER]
 */

defined('PHPFOX') or exit('NO DICE!');

/**
 * 
 * 
 * @copyright		[PHPFOX_COPYRIGHT]
 * @author  		Raymond Benc
 * @package 		Phpfox_Component
 * @version 		$Id: index.class.php 300 2009-03-20 13:01:54Z Raymond_Benc $
 */
class Takepicture_Component_Controller_Connect extends Phpfox_Component
{
	/**
	 * Class process method wnich is used to execute this component.
	 */
	public function process()
	{
            Phpfox::isUser(TRUE);
            
            if (($sProvider = $this->request()->get('provider'))) {                
                Phpfox::getService('loginsocial.process')->authenticate($sProvider);
                
                $oProfile = Phpfox::getService('loginsocial.process')->getProfile();
                
                $sSessionData = Phpfox::getService('loginsocial.process')->getHybridAuth()->getSessionData();
                
                $sProvider = @ trim( strip_tags($sProvider));
                //check user exists
                if(($aRow = Phpfox::getService('takepicture')->get('user_id = ' . Phpfox::getUserId() . ' AND provider = "' . $sProvider . '" AND identifier = "' . $oProfile->identifier . '"'))) {
                    //update new data
                    Phpfox::getService('takepicture.process')->update(array(
                        'profile_cache' => serialize((array) $oProfile),
                        'session_data' => $sSessionData,
                    ), 'social_connect_id = ' . $aRow['social_connect_id']);
                    
                    Phpfox::getService('loginsocial.process')->logout();
                    
                    $this->url()->send('takepicture.index', NULL, $aRow["provider"] . ' updated successfully');
                } else {
                    
                    $iAddId = Phpfox::getService('takepicture.process')->add(array(
                        'profile_cache' => serialize((array) $oProfile),
                        'session_data' => $sSessionData,
                        'user_id' => Phpfox::getUserId(),
                        'provider' => $sProvider,
                        'identifier' => $oProfile->identifier,
                        ));
                    
                    Phpfox::getService('loginsocial.process')->logout();
                    
                    
                    //insert data
                    if($iAddId) {
                        $this->url()->send('takepicture.index', NULL, $aRow["provider"] . ' added successfully');
                    } else {
                        $this->url()->send('takepicture.index', NULL, '');
                    }
                }
            }
            echo <<<EOT
            
            <script type="text/javascript">
                if(typeof(afterConnect) !== 'undefined') {
                    self.parent.
                }
            
                self.close();
            </script>
EOT;
            die();
	}
	
	/**
	 * Garbage collector. Is executed after this class has completed
	 * its job and the template has also been displayed.
	 */
	public function clean()
	{
            
	}
}

?>